package com.fernfx.xingtan.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.baidu.mapapi.map.Marker;
import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.entity.robredpacket.RobredPacketInfoEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.SJJNetworkProxy;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.robredpacket.ui.RobredPacketInfoActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2018/8/23.
 */

/*
     * 自定义dialog弹出框
     */
public class CustomDialog extends Dialog {
    private Context context;
    private Marker marker;
    public CustomDialog(Context context) {
        super(context, R.style.robred_packet_dialog_style);
        this.context = context;
    }

    public void setMarker(Marker marker){
        this.marker = marker;
    }

    public Marker getMarker(){
        return marker;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(context).inflate(R.layout.view_robred_packet_dialog, null, false);
        ViewGroup robredPacketBodyVw = view.findViewById(R.id.robred_packet_body_lat);
        ImageView closeDialogIv = view.findViewById(R.id.close_dialog_iv);
        ImageView openRobredPacketIv = view.findViewById(R.id.open_robred_packet_iv);
        openRobredPacketIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口然后跳转到指定的页面
                Map<String,Object> requestArgsMap = new HashMap<>(2);
                //请求参数
                Bundle bundle = marker.getExtraInfo();
                requestArgsMap.put("id",bundle.getInt("id"));
                //获取红包详情
                String url = OtherUtil.getGetUrl(Constant.RequestArgs.GET_ROBRED_PACKET_DETAILS_URL,requestArgsMap);
                SJJNetworkProxy.getInstance().get(url, null, new IRequestCallback() {
                    @Override
                    public void onSuccessful(NetworkOKResult result) {
                        String message = result.getMessage();
                        if (TextUtils.isEmpty(message)) {
                            onNetworkError(result.getMessage(), 500);
                            return;
                        }
                        RobredPacketInfoEntity robredPacketInfoEntity = FastJsonUtil.fromBean(message, RobredPacketInfoEntity.class);
                        if (null != robredPacketInfoEntity && OtherUtil.checkRequestStatus(robredPacketInfoEntity.getSuccess(),robredPacketInfoEntity.getStatus())) {
                            //讲联系人信息添加到联系人名称和联系人列表集合
                            RobredPacketInfoActivity.start(context,robredPacketInfoEntity);
                        } else {
                            ToastUtil.showCentertoast(robredPacketInfoEntity.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(String errorMessage, int errorCode) {
                        if (TextUtils.isEmpty(errorMessage)) {
                            onNetworkError(errorMessage,errorCode);
                            return;
                        }

                        final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                        if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                            ToastUtil.showCentertoast(baseEntity.getMsg());
                        }
                    }

                    @Override
                    public void onNetworkError(String errorMessage, int errorCode) {
                        ToastUtil.showCentertoast(R.string.network_error);
                    }
                });
            }
        });
        closeDialogIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(view);
    }
}
